package com.feihong.yw189.activity.shouye;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.feihong.yw189.R;
import com.feihong.yw189.activity.base.BaseActivity;
import com.feihong.yw189.adapter.ShouYePaiHangAdapter;
import com.feihong.yw189.app.App;
import com.feihong.yw189.listener.http.RequestCallBack;
import com.feihong.yw189.message.LoginMessage;
import com.feihong.yw189.model.PaiHangModel;
import com.holenzhou.pullrecyclerview.PullRecyclerView;
import com.holenzhou.pullrecyclerview.layoutmanager.XLinearLayoutManager;

import org.xutils.common.Callback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/4/13 0013.
 */

public class PaiHangActivity extends BaseActivity {


    @BindView(R.id.iv_lin)
    ImageView ivLink;
    @BindView(R.id.txt_pagetitle)
    TextView textTitle;
    @BindView(R.id.iv_back)
    ImageView ivWriteBack;
    @BindView(R.id.pull_recycler_view)
    PullRecyclerView mPullRecyclerView;
    @BindView(R.id.tvYesterday)
    TextView tvYestertady;
    @BindView(R.id.tvTerday)
    TextView tvTerday;
    @BindView(R.id.tvTerdayPai)
    TextView tvTerdayPai;

    @BindView(R.id.paihang_head_layout)
    AppBarLayout appBarLayout;
    List<PaiHangModel.DataBean.PaihangTodayBean> mDataList;
    ShouYePaiHangAdapter mAdapter;
    private int pageSize = 10;
    int page = 1;

    private Unbinder unbinder;

    private boolean isNeedClear = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_paihanglist);
        unbinder = ButterKnife.bind(this);
        initView();
        bindData();
    }

    @Override
    public void setTheme() {
        isDrakFont = false;
    }

    private void initView() {
        ivLink.setVisibility(View.GONE);
        setPageTitle("排行榜");

        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) appBarLayout.getLayoutParams();
        layoutParams.setMargins(layoutParams.leftMargin, layoutParams.topMargin + getStatusBarHeight(),
                layoutParams.rightMargin, layoutParams.bottomMargin);
        //  App.getApp().getHttpTools().getPaiHang(String.valueOf(page),String.valueOf(pageSize), callBackUp);
    }
    private  int getStatusBarHeight() {
        // 获得状态栏高度
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        return getResources().getDimensionPixelSize(resourceId);
    }
    private void bindData() {
        mDataList = new ArrayList<>();
        // 初始化PullRecyclerView
        mPullRecyclerView.setLayoutManager(new XLinearLayoutManager(this));
        mAdapter = new ShouYePaiHangAdapter(this, R.layout.item_paihang, mDataList);
        mPullRecyclerView.setAdapter(mAdapter);

        mPullRecyclerView.setColorSchemeResources(R.color.appBlue);
        // 设置下拉刷新的旋转圆圈的颜色
        mPullRecyclerView.enablePullRefresh(true);
        // 开启下拉刷新，默认即为true，可不用设置
        mPullRecyclerView.enableLoadMore(false);
        mPullRecyclerView.enableLoadDoneTip(false, R.string.load_done_tip);
        // 开启数据全部加载完成时的底部提示，默认为false
        //刷新监听
        mPullRecyclerView.setOnRecyclerRefreshListener(new PullRecyclerView.OnRecyclerRefreshListener() {
            @Override
            public void onPullRefresh() {

                // 下拉刷新网络请求
                mPullRecyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        isNeedClear = true;
                        //网络请求
                        paihangCallBack = App.getApp().getHttpTools().getPaiHang(String.valueOf(page), String.valueOf(pageSize), callBackUp);
                    }
                }, 1000);
            }

            @Override
            public void onLoadMore() {
            }
        });
        mPullRecyclerView.postRefreshing();
        mPullRecyclerView.stopLoadMore();
    }

    Callback.Cancelable paihangCallBack;
    @Override
    public void onUserLogin(LoginMessage loginMessage) {

    }

    @OnClick({R.id.iv_back})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            default:
                break;
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }

    RequestCallBack<String> callBackUp = new RequestCallBack<String>() {
        @Override
        public void onSuccess(String result) {
            if(activityIsDestory()){
                return;
            }
            mPullRecyclerView.stopRefresh();
            try {
                //json解析
                PaiHangModel paiHangModel = JSON.parseObject("" + result, PaiHangModel.class);
                if (paiHangModel != null) {
                    if (paiHangModel.getStatus() == 1) {
                        //list解析
                        if (paiHangModel.getData() != null) {
                    /*昨日排行*/
                            if (paiHangModel.getData().getPaihang_my_yesterday() != null) {
                                tvYestertady.setText("" + paiHangModel.getData().getPaihang_my_yesterday().getCount());
                            } else {
                                tvYestertady.setText("0");
                            }
                     /*今日排行*/
                            if (paiHangModel.getData().getPaihang_my_today() != null) {
                                tvTerday.setText("" + paiHangModel.getData().getPaihang_my_today().getMingci());
                            } else {
                                tvTerday.setText("0");
                            }
                       /*我的今日订单*/
                            if (paiHangModel.getData().getPaihang_my_today() != null) {
                                tvTerdayPai.setText("" + paiHangModel.getData().getPaihang_my_today().getCount());
                            } else {
                                tvTerdayPai.setText("0");
                            }
                     /*今日订单排行*/
                            if (paiHangModel.getData().getPaihang_today().size() > 0) {
                                PaiHangModel.DataBean.PaihangTodayBean model;
                                if(isNeedClear){
                                    mDataList.clear();
                                    isNeedClear = false;
                                }
                                for (PaiHangModel.DataBean.PaihangTodayBean message : paiHangModel.getData().getPaihang_today()) {

                                    model = new PaiHangModel.DataBean.PaihangTodayBean();
                                    model.setAgent_name(message.getAgent_name());
                                    model.setCount(message.getCount());
                                    model.setMingci(message.getMingci());
                                    model.setMobile(message.getMobile());
                                    model.setUser_img(message.getUser_img());
                                    model.setUser_name(message.getUser_name());
                                    model.setRegion_name(message.getRegion_name());
                                    mDataList.add(model);
                                }
                                mAdapter.notifyDataSetChanged();
                                // 当剩余还有大于0页的数据时，开启上拉加载更多
                            } else {
                                showToast("暂无更多排行信息");
                            }
                        } else {
                            showToast("暂无更多排行信息");
                        }
                    } else {
                        showToast(paiHangModel.getInfo());
                    }
                } else {
                    showToast("暂无更多排行信息");
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        @Override
        public void onError(Throwable ex, boolean isOnCallback) {
            if(activityIsDestory()){
                return;
            }
            mPullRecyclerView.stopRefresh();
            showToast(getResources().getString(R.string.connect_sever_error_toast) );
        }

        @Override
        public void onCancelled(Callback.CancelledException cex) {
        }

        @Override
        public void onFinished() {
        }
    };

}
